﻿using System;
using System.Globalization;
using System.Web.UI;
using Module;
using Module.QuanTriHeThong.DanhMuc;

namespace HospitalManager.Site
{
    public partial class ThongTinCaNhan : Page
    {
        private readonly DBInteractionBase _dbInteractionBase = new DBInteractionBase();

        private DMTaiKhoan _dmTaiKhoan = new DMTaiKhoan();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            if (Session["username"] == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                LoadPage(Session["username"].ToString().Trim());
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ViewState["CheckRefresh"] = Session["CheckRefresh"];
        }

        private void LoadPage(string username)
        {
            var dataThongTinCaNhan = _dmTaiKhoan.SelectById(username);
            if (dataThongTinCaNhan.Rows.Count <= 0) return;
            txtHoTen.Text = dataThongTinCaNhan.Rows[0]["HoTen"].ToString().Trim();
            txtDiaChi.Text = dataThongTinCaNhan.Rows[0]["DiaChi"].ToString().Trim();
            txtSoDienThoai.Text = dataThongTinCaNhan.Rows[0]["SoDienThoai"].ToString().Trim();
            Session["CheckRefresh"] = Server.UrlEncode(DateTime.Now.ToString(CultureInfo.InvariantCulture));
        }

        private void ResetPage(string username)
        {
            txtMatKhau.Text = string.Empty;
            txtMatKhauMoi.Text = string.Empty;
            txtNhapLaiMatKhauMoi.Text = string.Empty;
            LoadPage(username);
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            var username = Session["username"];
            if (username == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                if (Session["CheckRefresh"].ToString() != ViewState["CheckRefresh"].ToString())
                {
                    ResetPage(username.ToString().Trim());
                }
                else
                {
                    bool status;
                    _dmTaiKhoan.KiemTraTaiKhoan(username.ToString().Trim(),
                        _dbInteractionBase.Encrypt(txtMatKhau.Text.Trim()), false, out status);
                    if (status)
                    {
                        if (_dmTaiKhoan.DoiMatKhau(username.ToString().Trim(),
                            _dbInteractionBase.Encrypt(txtMatKhauMoi.Text.Trim())))
                        {
                            Response.RedirectToRoute("DangNhap");
                        }
                        else
                        {
                            ClientScript.RegisterClientScriptBlock(typeof(string), "alert",
                                "bootbox.alert('Đổi mật khẩu thất bại, vui lòng thử lại!');", true);
                        }
                    }
                    else
                    {
                        ClientScript.RegisterClientScriptBlock(typeof(string), "alert",
                            "bootbox.alert('Mật khẩu hiện tại không đúng, vui lòng thử lại!');", true);
                    }
                    ResetPage(username.ToString().Trim());
                }
            }
        }

        protected void btnLuu_ThongTinCaNhan_Click(object sender, EventArgs e)
        {
            var username = Session["username"];
            if (username == null)
            {
                Response.RedirectToRoute("DangNhap");
            }
            else
            {
                if (Session["CheckRefresh"].ToString() != ViewState["CheckRefresh"].ToString())
                {
                    ResetPage(username.ToString().Trim());
                }
                else
                {
                    _dmTaiKhoan = new DMTaiKhoan
                    {
                        TaiKhoan = username.ToString().Trim(),
                        HoTen = txtHoTen.Text.Trim(),
                        DiaChi = txtDiaChi.Text.Trim(),
                        SoDienThoai = txtSoDienThoai.Text.Trim()
                    };
                    if (_dmTaiKhoan.Update(_dmTaiKhoan))
                    {
                        Response.Redirect("/trang-chu");
                    }
                    else
                    {
                        ClientScript.RegisterClientScriptBlock(typeof(string), "alert",
                            "bootbox.alert('Cập nhật thông tin thất bại, vui lòng thử lại!');", true);
                    }
                    ResetPage(username.ToString().Trim());
                }
            }
        }
    }
}